package com.ysd.lis.service.bac.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacReqPurpose;
import com.ysd.lis.entity.bac.BacReqPurposeDetail;
import com.ysd.lis.entity.bac.BacReqPurposeFeeItem;
import com.ysd.lis.entity.bac.BacReqPurposeSampletype;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.bac.BacReqPurposeSampletypeMapper;
import com.ysd.lis.request.bac.PurposeItemParam;
import com.ysd.lis.service.bac.BacReqPurposeSampletypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 检验目的对应标本类型 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-10-17
 */
@Service
public class BacReqPurposeSampletypeServiceImpl extends ServiceImpl<BacReqPurposeSampletypeMapper, BacReqPurposeSampletype> implements BacReqPurposeSampletypeService {

    @Autowired
    private BacReqPurposeSampletypeMapper bacReqPurposeSampletypeMapper;
    @Autowired
    private BacReqPurposeSampletypeService bacReqPurposeSampletypeService;

    @Override
    public Result findPurposeSampletypeList(PurposeItemParam param) {
        MPJLambdaWrapper<BacReqPurposeSampletype> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacReqPurposeSampletype.class);
        queryWrapper.selectAs("sa", LabSampleType::getSampleTypeName, BacReqPurposeSampletype::getSampleTypeName);
        queryWrapper.leftJoin(BacReqPurpose.class, "pu", p -> p.eq(BacReqPurpose::getPurposeNo, BacReqPurposeSampletype::getPurposeNo).eq(BacReqPurpose::getDelFlag, 0));
        queryWrapper.leftJoin(LabSampleType.class, "sa", p -> p.eq(LabSampleType::getSampleTypeCode, BacReqPurposeSampletype::getSampleTypeCode).eq(LabSampleType::getDelFlag, 0));
        queryWrapper.eq(BacReqPurposeSampletype::getDelFlag, 0);
        queryWrapper.like(StringUtils.isNotBlank(param.getSeachValue()), BacReqPurposeSampletype::getPurposeNo, param.getSeachValue());
        queryWrapper.and(p -> p.eq(BacReqPurposeSampletype::getPurposeNo, param.getPurposeNo()).eq(BacReqPurposeSampletype::getDelFlag, 0));
        queryWrapper.orderByAsc(BacReqPurposeSampletype::getSeq);
        List<BacReqPurposeSampletype> detailList = bacReqPurposeSampletypeMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result addPurposeSampletype(List<BacReqPurposeSampletype> bacReqPurposeSampletype) {
        bacReqPurposeSampletypeService.remove(new LambdaQueryWrapper<>(BacReqPurposeSampletype.class).eq(BacReqPurposeSampletype::getPurposeNo, bacReqPurposeSampletype.get(0).getPurposeNo()));
        List<BacReqPurposeSampletype> distinctList = bacReqPurposeSampletype.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(BacReqPurposeSampletype::getSampleTypeCode, item -> item, (existing, replacement) -> existing), map -> new ArrayList<>(map.values())
                ));
        boolean b = bacReqPurposeSampletypeService.saveBatch(distinctList);
        if (b) {
            return Result.succ(1, "操作成功", distinctList);
        } else {
            return Result.fail(400, "操作失败", "");
        }
    }

    @Override
    public Result deleteBatchPurposeSampletype(List<BacReqPurposeSampletype> bacReqPurposeSampletype) {
        List<String> ids = bacReqPurposeSampletype.stream().map(BacReqPurposeSampletype::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
